import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
    //////数据存放
let state = {
        categorylist: [],
        zhanglei_header: false,
        cartList: [],
        user:{
          name:"",
          pasw:""
        },
        flag:0,
        index:0,
        all:false
}
    //////数据处理
const mutations = {
        toCar(state,n){
          state.flag=false;
          state.index=0;
          let list = {
            iid:n.iid,
            lowPrice:n.lowPrice,
            title:n.title,
            style:n.style,
            size:n.size,
            cartNum:n.cartNum,
            img:n.topImages[0],
            self:n.self
          }
          if(state.cartList.length==0){
            state.cartList.push(list)
          }else{
            for(var i=0;i<state.cartList.length;i++){
              if(state.cartList[i].iid===list.iid&&state.cartList[i].style===list.style&&state.cartList[i].size===list.size){
                 state.index = i;
                 state.flag = 1;
                 break;
              }else{
                 continue;
              }
            }
            if(state.flag==1){
                state.cartList[state.index].cartNum += list.cartNum;
            }else{
               state.cartList.push(list)
            }
          }
          console.log(state.cartList)
        },
        add(state,n) {
            state.cartList[n].cartNum++;
        },
        reduce(state,n) {
            state.cartList[n].cartNum--;
            if (state.cartList[n].cartNum < 1) {
                state.cartList.splice(n,1)
            }
        },
        del(state) {
            for(var i=state.cartList.length-1;i>=0;i--){
                if(state.cartList[i].self==true){
                   state.cartList.splice(i,1);
                }
            }  
        },
        check(state,index){
            let count =0;
            state.cartList[index].self = !state.cartList[index].self;
            for(var i=0;i<state.cartList.length;i++){
                if(state.cartList[i].self==true){
                  count++;
                }
            }
            if(count==state.cartList.length){
              state.all = true;
            }else{
              state.all = false;
            }
        },
        change(){
           state.all = !state.all;
           for(var i=0;i<state.cartList.length;i++){
              state.cartList[i].self = state.all;
           }
        },
        //categorylink(state,link){
        //    console.log(link)
        //    let num=link.indexOf('pid=');
        //    let linknum=link.slice(num+4,num+9);
        ///   state.link=linknum
        ///\ }
        setHeader(Store) {
            state.zhanglei_header = !state.zhanglei_header
        }
    }
    //////事件处理
const actions = {
    //categorylink:({commit},link)=>{
    //console.log(1111,link)
    //commit('categorylink('+link+')')
    //  commit('categorylink',link)
    //  }
};
const getters = {
    // link(state){
    //     return state.link
    // },
    categorylist(state) {
        return state.categorylist;
    },
    price(state){
        let money =0;
        for(var i=0;i<state.cartList.length;i++){
            if(state.cartList[i].self==true){
               money += state.cartList[i].cartNum*state.cartList[i].lowPrice;
            }
        }
        return money.toFixed(2);
     },
     num(state){
         let num = 0;
         for(var i=0;i<state.cartList.length;i++){
            num +=state.cartList[i].cartNum;
         }
         return num;
     },
     num1(state){
         let num1 = 0;
         for(var i=0;i<state.cartList.length;i++){
            if(state.cartList[i].self==true){
              num1+=state.cartList[i].cartNum;
            }
         } 
         return num1;
     },
     dingdan(state){
        let shopList = [];
        for(var i=0;i<state.cartList.length;i++){
          if(state.cartList[i].self==true){
            shopList.push(state.cartList[i])
          }
        }
        return shopList;
     }
};
export default new Vuex.Store({
    state,
    mutations,
    actions,
    getters
})